/**
 * 是否整数
 * @param number
 */
export const isInteger = function (number: unknown): number is number {
    return Number.isInteger(number)
}

/**
 * 是否正整数
 * @param number
 */
export const isPositiveInteger = function (number: unknown): number is number {
    return isInteger(number) && number > 0
}

/**
 * 是否0或者正整数
 * @param number
 */
export const isZeroOrPositiveInteger = function (number: unknown): number is number {
    return number === 0 || isPositiveInteger(number)
}

/**
 * 是否是空值
 * @param value
 */
export const isEmpty = function (value: unknown): boolean {
    if (typeof value === 'number' && Number.isNaN(value)) {
        return true
    }
    return value === null || value === undefined || value === ''
}

/**
 * 是否是是价格
 * @param value
 */
export const isPrice = function (value: unknown): boolean {
    if (typeof value === 'number' || typeof value === 'string') {
        const price = Number.parseFloat(`${value}`)
        return !Number.isNaN(price) && Math.floor(price * 100) === price * 100
    }

    return false
}

/**
 * 是否手机号
 * @param value

 */
export const isPhone = function (value: string): boolean {
    return typeof value === 'string' ? /^1[2-9]\d{9}$/.test(value) : false
}

/**
 * 验证身份证号码
 */
export function isIdCard(value: string) {
    return typeof value === 'string'
        ? // eslint-disable-next-line regexp/no-unused-capturing-group
          /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|12]\d)|3[01])\d{3}([0-9X])$/.test(value)
        : false
}

/**
 * 验证纯数字
 */
export function isNumber(value: string) {
    return typeof value === 'string' ? /\d/.test(value) : false
}

export const isHttpLink = function (value: string): boolean {
    return typeof value === 'string'
        ? /^https?:\/\/[\da-z.-]+\.[a-z]{2,6}(?::\d{1,5})?(?:\/\S*)?$/i.test(value)
        : false
}
